package com.finance.cooperate.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName StringUtil
 * @Description 字符串常用工具类
 * @Author shen
 * @Date 2022/5/25 15:23
 * @Modify ...
 */
@SuppressWarnings("all")
public final class StringUtil {

    /**
    * @Author shenanxin
    * @Description 判断字符串中是否包含中文
    * @Date 14:43 2023/9/20
    * @Param [text]
    * @return boolean
    **/
    public static boolean hasChinese(String text) {
        Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5]");
        Matcher matcher = pattern.matcher(text);
        return matcher.find();
    }


    /**
     * @Author shen
     * @Description 判断一个对象及对象里的字符串属性是否为空
     * @Date 5:55 下午 2020/6/11
     * @Param [obj, otherObj]
     * @return boolean
     **/
    public static boolean isEmptyOrNvl(Object obj, Object... otherObj) {
        if (obj == null || "null".equals(obj)) {
            return true;
        } else {
            if (obj.getClass().isAssignableFrom(String.class)) {
                if (String.valueOf(obj).trim().equals("")) {
                    return true;
                }
            }
            if (otherObj.length > 0) {
                for (Object othObj : otherObj) {
                    if (isEmptyOrNvl(othObj)) {
                        return true;
                    }
                }
            }
            return false;
        }
    }

    /**
     * @Author shen
     * @Description 属性值转成特征字符串
     * @Date 5:58 下午 2020/6/11
     * @Param [obj]
     * @return java.lang.String
     **/
    public static String obj2Feature(Object obj) {
        boolean f = isEmptyOrNvl(obj);
        if (!f) {
            if (obj instanceof Number) {
                return String.valueOf(obj);
            } else if (obj instanceof String) {

                String str = String.valueOf(obj).trim();

                Boolean isNumber = str.matches("-?[0-9]+\\.?[0-9]*");

                if (isNumber) {
                    return str;
                }

            }
        }

        return "-999";
    }

    /**
     * @Author shen
     * @Description 扁平化输出 json
     * @Date 4:40 下午 2022/3/3
     * @Param [json]
     * @return java.util.Map<java.lang.String,java.lang.Object>
     **/
    public static Map<String, Object> jsonConvertMap(String jsonStr){

        JSONObject json = JSON.parseObject(jsonStr);

        Map<String, Object> map = new HashMap<>();
        if(json != null && json.size() > 0){
            for(String k : json.keySet()){
                Object v = json.get(k);
                if(v instanceof JSONArray){
                    List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
                    Iterator<Object> it = ((JSONArray)v).iterator();
                    while(it.hasNext()){
                        JSONObject json2 = (JSONObject) it.next();
                        list.add(jsonConvertMap(json2.toJSONString()));
                    }
                    map.put(k, list);
                } else if(v instanceof JSONObject){
                    List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
                    JSONObject child = (JSONObject) v;
                    for(String stk: child.keySet()){
                        Object value = child.get(stk);
                        if(value instanceof JSONArray){
                            Iterator<Object> it = ((JSONArray)value).iterator();
                            while(it.hasNext()){
                                JSONObject json2 = (JSONObject) it.next();
                                list.add(jsonConvertMap(json2.toJSONString()));
                            }
                        }else{
                            map.put(stk, value.toString());
                        }
                    }
                    if(list.size() > 0){
                        for(int m = 0;m<list.size();m++){
                            Map<String, Object> chMap = list.get(m);
                            for(String chKey : chMap.keySet()){
                                map.put(chKey, chMap.get(chKey).toString());
                            }
                        }
                    }
                }else{
                    map.put(k, v);
                }
            }
        }
        return map;
    }


    /**
     * @Author shen
     * @Description 从字符串中提取数字
     * @Date 2:27 下午 2022/3/4
     * @Param [str]
     * @return int
     **/
    public static int digitalExtraction(String str) {

        int result = -999;

        String REGEX = "[^0-9]";

        if (!StringUtil.isEmptyOrNvl(str)) {
            String s = Pattern.compile(REGEX).matcher(str).replaceAll("").trim();

            if (!StringUtil.isEmptyOrNvl(s)) {
                result = Integer.valueOf(s);
            }
        }

        return result;


    }

    /**
     * @Author shen
     * @Description 从字符串中提取数字
     * @Date 2:27 下午 2022/3/4
     * @Param [str]
     * @return int
     **/
    public static Long digitalLongExtraction(String str) {

        Long result = -999L;

        String REGEX = "[^0-9]";

        if (!StringUtil.isEmptyOrNvl(str)) {
            String s = Pattern.compile(REGEX).matcher(str).replaceAll("").trim();

            if (!StringUtil.isEmptyOrNvl(s)) {
                result = Long.valueOf(s);
            }
        }

        return result;


    }


    /**
     * @Author shen
     * @Description 判断字符串是否包含字母
     * @Date 6:06 下午 2022/5/10
     * @Param [cardNum]
     * @return boolean
     **/
    public static boolean judgeContainsStr(String str) {
        String regex=".*[a-zA-Z]+.*";
        Matcher m=Pattern.compile(regex).matcher(str);
        return m.matches();
    }


}
